/*
 * Copyright: Tianjin Berheley Tech Co,.ltd, China
 * berheleyBI_head
 * com.berheley.bi.exception -> BerheleyBIHandlerExceptionResolver.java
 * Created on 2009-9-8-下午04:50:04
 */
package com.webframe.exception;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.webframe.constant.WFConstant;


/**
 * 类功能描述：
 *
 * @author      
 * Create:      2009-9-8 下午04:50:04
 * Description: 
 */
public class WebframeHandlerExceptionResolver implements HandlerExceptionResolver
{

	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerExceptionResolver#resolveException(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	@SuppressWarnings({
				"rawtypes", "unchecked"})
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
	{
		String errorPage = request.getParameter(WFConstant.Constant_Error_PAGE_);
		Map params=request.getParameterMap();
		Iterator it = params.keySet().iterator();
		Map object = new HashMap();
		Object tmp;
		while(it.hasNext())
		{
			tmp=it.next();
			object.put(tmp, ((Object [])params.get(tmp))[0]);
		}
		object.remove(WFConstant.Constant_PK_);
		object.put(WFConstant.Constant_Duplicate_Submission_KEY_, request.getSession().getAttribute(WFConstant.Constant_Duplicate_Submission_KEY_));
		ModelAndView mv = new ModelAndView(errorPage,object);
		if(ex instanceof BusinessException)
		{
			BusinessException ex1=(BusinessException)ex;
			Map messages = new HashMap();
			messages.put(ex1.getErrorKey(), ex1.getErrorCode());
			mv.addObject(WFConstant.Constant_MESSAGE_FOR_USER_, messages);
		}
		else
		{
			mv.addObject(WFConstant.Constant_MESSAGE_FOR_ADMIN_, ex.toString());
		}
		
		return mv;
	}

}
